package com.ganmiao.repository;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.ganmiao.resp.PageResult;
import com.ganmiao.utils.PageListUtil;
import org.springframework.beans.factory.annotation.Autowired;


/**
 * @Author zero.yin
 * @Date 2022/7/22 3:02 PM
 * @Desc
 */
public class BaseRepositoryImpl<M extends BaseMapper<T>, T> extends ServiceImpl<BaseMapper<T>, T> {

    @Autowired
    protected M baseMapper;

    public BaseRepositoryImpl() {
    }

    public M getBaseMapper() {
        return this.baseMapper;
    }

    public PageResult<T> queryPage(Integer pageIndex, Integer pageSize, Wrapper<T> queryWrapper) {
        IPage<T> page = new Page<>();
        page.setCurrent(pageIndex);
        page.setSize(pageSize);
        IPage<T> iPage = this.baseMapper.selectPage(page, queryWrapper);
        //多租户：重新查询总数
        Long totalCount = baseMapper.selectCount(queryWrapper);
        iPage.setTotal(totalCount);

        return PageListUtil.getPageResult(iPage);
    }

}
